﻿#region Usings

using System.Windows.Controls;
using SilverBox.Behaviors;
using SilverBox.Extensions;
using SlDataGrid = System.Windows.Controls.DataGrid;

#endregion

namespace SilverBox.Controls
{
    public class DataGrid : SlDataGrid
    {
        #region Delegates

        public delegate void RowDoubleClickHandler(object sender, DoubleClickEventArgs args);

        #endregion

        public DataGrid()
        {
            LayoutUpdated += (obj, args) =>
                                 {
                                     var rows = this.GetRows();
                                     rows.ForEach(row =>
                                                      {
                                                          var behaviors = row.GetBehaviors();
                                                          behaviors.GetBehaviorsByType<MouseDoubleClickBehavior>().
                                                              ForEach(b => behaviors.Remove(b));
                                                          var doubleClickBehavior = new MouseDoubleClickBehavior();
                                                          doubleClickBehavior.MouseDoubleClick +=
                                                              (objB, argsB) => OnRowDoubleClick(row);
                                                          behaviors.Add(doubleClickBehavior);
                                                          behaviors.Attach(row);
                                                      });
                                 };

            var thisBehaviors = this.GetBehaviors();
            thisBehaviors.Add(new MouseWheelScrollBehavior());
            thisBehaviors.Attach(this);
        }

        private void OnRowDoubleClick(DataGridRow row)
        {
            if (RowDoubleClick != null)
                RowDoubleClick(this, new DoubleClickEventArgs(row));
        }

        public event RowDoubleClickHandler RowDoubleClick;
    }
}